package cn.tedu.overhaul.management.dto.persist.repository.impl;

import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulMapper;
import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulRentMapper;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulRentRepository;
import cn.tedu.overhaul.management.dto.persist.repository.IOverhaulRepository;
import cn.tedu.overhaul.management.pojo.dto.OverhaulRentUpdateAllDTO;
import cn.tedu.overhaul.management.pojo.dto.OverhaulRentUpdateDTO;
import cn.tedu.overhaul.management.pojo.entity.Overhaul;
import cn.tedu.overhaul.management.pojo.entity.OverhaulRent;
import cn.tedu.overhaul.management.pojo.vo.*;
import cn.tedu.rtms.commons.ex.ServiceException;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.util.PageInfoToPageDataConverter;
import cn.tedu.rtms.commons.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class OverhaulRentRepositoryImpl implements IOverhaulRentRepository {
    @Autowired
    OverhaulRentMapper overhaulRentMapper;

    @Autowired
    OverhaulMapper overhaulMapper;

    @Override
    public Long selectByName(String name) {
        log.debug("开始执行【根据名字查询id】的数据访问，参数：{}",name);
        return overhaulRentMapper.selectByName(name);
    }

    @Override
    public int insert(OverhaulRent overhaulRent) {
        log.debug("开始执行【插入借用记录】的数据访问，参数：{}",overhaulRent);
        return overhaulRentMapper.insert(overhaulRent);
    }

    @Override
    public PageData<RentVO> selectRent(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询借用记录表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<RentOriginalVO> listOriginal=overhaulRentMapper.selectRent();
        log.debug("listOriginal={}",listOriginal);
        log.debug("listOriginal.length={}",listOriginal.size());

        PageInfo<RentOriginalVO> pageInfoOriginal = new PageInfo<>(listOriginal);
        long total = pageInfoOriginal.getTotal();
        int pages = pageInfoOriginal.getPages();


        List<RentVO> list=new ArrayList<>();


        for (RentOriginalVO rentOriginalVO : listOriginal) {
            log.debug("rentOriginalVO={}",rentOriginalVO);
            RentVO rentVO=new RentVO();
            rentVO.setId(rentOriginalVO.getId());
            String name=overhaulRentMapper.selectById(rentOriginalVO.getUserId());
            rentVO.setName(name);
            List<ToolNameNumberVO> nameNumber =overhaulRentMapper.selectByToolId(rentOriginalVO.getToolId());
            log.debug("nameNumber={}",nameNumber);
            for (ToolNameNumberVO toolNameNumberVO : nameNumber) {
                rentVO.setToolName(toolNameNumberVO.getName());
                rentVO.setToolNumber(toolNameNumberVO.getNumber());
            }
            rentVO.setStart(rentOriginalVO.getStart());
            if(rentOriginalVO.getEnd()==null||rentOriginalVO.getEnd().isEmpty()){
                rentVO.setEnd("未归还");
                rentVO.setIsReturned(false);
                overhaulMapper.updateStatus(2,rentOriginalVO.getToolId());
                log.debug("if开始处理[更改检修设备表状态]完成");
            }else{
                rentVO.setEnd(rentOriginalVO.getEnd());
                rentVO.setIsReturned(true);
            }

            list.add(rentVO);
        }
        log.debug("list={}",list);



        PageInfo<RentVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total); // 设置正确的总记录数
        pageInfo.setPages(pages); // 设置正确的总页数
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        log.debug("pageInfo={}",pageInfo);
        PageData<RentVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("pageData={}",pageData);



        return pageData;
    }

    @Override
    public int updateEndTime(Long id) {
        log.debug("开始执行【归还工具】的数据访问，参数：{}",id);
        int updates=overhaulRentMapper.updateEndTime(id);
        if(updates==0){
            String message ="根据ID归还失败，此ID的工具不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return updates;
    }

    @Override
    public RentOriginalVO getStandardById(Long id) {
        log.debug("开始执行【根据ID修改借用记录查询】的数据访问，参数：{}", id);
        RentOriginalVO rentOriginalVO=overhaulRentMapper.getStandardById(id);


        return rentOriginalVO;
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除借用记录】的数据访问，参数：{}", id);
        return overhaulRentMapper.deleteById(id);
    }


    @Override
    public int update(OverhaulRentUpdateAllDTO overhaulRentUpdateAllDTO) {
        log.debug("开始执行【根据ID修改借用记录】的数据访问，参数：{}", overhaulRentUpdateAllDTO);

        return overhaulRentMapper.update(overhaulRentUpdateAllDTO);
    }


}
